<?php
class WorkLogController extends CommonController {
	private $join;
	private $db;
	private function search_before(){
		$this->db = M (array($this->table=>'w'));
		$this->join='inner join '.C('DB_PREFIX').'user u on u.id=w.user_id 
				inner join '.C('DB_PREFIX').'product p on p.id=w.product_id
				inner join '.C('DB_PREFIX').'process pc on pc.id=w.process_id
				';
		$this->where='1=1';
		if(isset($_GET['name'])&&!empty($_GET['name'])){
			$this->where="u.name like '%{$_GET['name']}%'";
		}
		if(isset($_GET['order_id'])&&!empty($_GET['order_id'])){
			$this->where.=" and w.order_id like '%{$_GET['order_id']}%'";
		}
		if(isset($_GET['product_name'])&&!empty($_GET['product_name'])){
			$this->where.=" and p.name like '%{$_GET['product_name']}%'";
		}
		if(isset($_GET['process_name'])&&!empty($_GET['process_name'])){
			$this->where.=" and pc.name like '%{$_GET['process_name']}%'";
		}
		if(isset($_GET['date'])&&!empty($_GET['date'])){
			$start_time=strtotime($_GET['date']);
			$end_time=$start_time+86399;
			$this->where.=" and ((w.start_time>={$start_time} and w.end_time<={$end_time}) or (w.start_time<={$start_time} and w.end_time>={$end_time}))";
		}else if(isset($_GET['start_time'])&&!empty($_GET['start_time'])&&!empty($_GET['end_time'])){
			$start_time=strtotime($_GET['start_time']);
			$end_time=strtotime($_GET['end_time']);
			$this->where.=" and ((w.start_time>={$start_time} and w.end_time<={$end_time}) or (w.start_time<={$start_time} and w.end_time>={$end_time}))";
		}
	}
	public function index() {
		$this->search_before();
		$count = $this->db->where($this->where)->join($this->join)->count (); // 计算总数
		$p = new Page ( $count);
		$list = $this->db->field('w.*,u.name,p.name as product_name,pc.name as process_name')->order ('w.id desc')->join($this->join)->where($this->where)->limit ( $p->firstRow . ',' . $p->listRows )->select ();
		$page = $p->show ();
		$this->assign ( "page", $page );
		$this->assign ( "list", $list );
		$admin_list=M('Admin')->twoArrayField('id')->field('id,name')->select();
		$this->assign('admin_list',$admin_list);
		$this->display ( $this->tpl );
	}
	public function output() {
		set_time_limit(0);
		$this->search_before();
		$list = $this->db->field('w.*,u.name,p.name as product_name,pc.name as process_name')->order ('w.id desc')->join($this->join)->where($this->where)->select ();
		$admin_list=M('Admin')->twoArrayField('id')->field('id,name')->select();
		$excel=new Excel();
		$data=array();
		$header=array('order_id'=>'Production order','name'=>'Name','date'=>'Date','start_time'=>'Start Time','end_time'=>'End Time','spend_time'=>'Spend Time（h）','product_name'=>'P/N','process_name'=>'Process Name','panel_no'=>'Panel No','remark'=>'Remark','create_time'=>'Input Time','admin_name'=>'Input Admin');
		foreach($list as $k=>$v){
			$data[$k]['order_id']=$v['order_id'];
			$data[$k]['name']=$v['name'];
			$data[$k]['date']=date('Y-m-d',$v['start_time']);
			$data[$k]['start_time']=date('H:i',$v['start_time']);
			$data[$k]['end_time']=date('H:i',$v['end_time']);
			$data[$k]['spend_time']=show_time($v['start_time'],$v['end_time']);
			$data[$k]['panel_no']=$v['panel_no'];
			$data[$k]['product_name']=$v['product_name'];
			$data[$k]['process_name']=$v['process_name'];
			$data[$k]['remark']=$v['remark'];
			$data[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
			$data[$k]['admin_name']=$admin_list[$v['admin_id']]['name'];
		}
		$excel->create($data,$header);
		exit('导出成功');
	}
	
	public function updateBefore() {
		$this->saveBefore ();
	}
	public function saveBefore() {
		if ($this->isGet ()) {
			$user_list = M ( 'User' )->twoArrayField('id')->field ( 'id,name' )->select ();
			$this->assign ( 'user_list', $user_list );
			$product_list = M ( 'Product' )->twoArrayField('id')->select ();
			$this->assign ( 'product_list', $product_list );
			$process_list = M ( 'Process' )->twoArrayField('id')->select ();
			$this->assign ( 'process_list', $process_list );
		}
	}
}